/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
* @Element Elemento basico
*
* @version 0.1
*/
/**
* Muestra un mensaje de texto
* @param {String} metodo nombre del metodo
* @param {String} mensaje mensaje a mostrar
* @returns {integer} el codigo de retorno 0
*/
var _idCounter = 0;

Element.prototype._id = '';
Element.prototype.width = '';
Element.prototype.height = '';
Element.prototype.style = '';
Element.prototype.Classes = [];
Element.prototype.rendered = '';
Element.prototype.listeners = {};
Element.prototype.name = '';
Element.prototype.Dom = '';
Element.prototype.renderTo = '';
Element.prototype.type = '';


Element.prototype._marginLeft = 5;

Element.prototype.rckItems = {};

Element.prototype._deep = 1;

/*
 *Twitter Bootstrap properties
 */
//tipo de class btn-primary, btn-info
Element.prototype._btClassType = '';   
Element.prototype._btSize = '';   

Element.prototype.CLASSNAMES = {
    CONTAINER:'rck-container rck-container-background-color',
    CONTAINER_TITLE:'rck-container-title modal-header',
    CONTAINER_BODY:'rck-container-body',
    CONTAINER_BUTTONS_AREA:'rck-container-buttons-area',
    CONTAINER_FORM_HBOX:'form-horizontal',
    CONTAINER_FORM_VBOX:'form-inline',
    SUB_CONTAINER_TITLE:'rck-sub-container-title',
    VBOX:'rck-layout-vbox',
    HBOX:'rck-layout-hbox',
    BUTTON:'rck-button btn',
    TEXTFIELD_HBOX:'rck-textfield-hbox',
    TEXTFIELD_VBOX:'rck-textfield-vbox',
    GRID:'rck-grid',
    GRID_SEARCH_SELECT:'rck-grid-search-select',
    GRID_SEARCH_INPUT:'rck-grid-search-input',
    GRID_TITLE:'rck-grid-title',
    FORM:'rck-form',
    BT_ROW:'row'
};

Element.prototype.json_encode = function(obj){
    if(obj instanceof Object)
        return JSON.stringify(obj);
    else throw '(Rocket) - Error encode to JSON String, te attribute it not a Object';
}

Element.prototype.json_decode = function(obj){
    if (obj instanceof String)
        return eval('('+obj+')');
    else throw '(Rocket) - Error decode to JSON Object, te attribute it not a String';
}

Element.prototype.jID = function(){
    return "#"+this.id;
}

Element.prototype.addItem = function(item){
    this.rckItems[item.id] = item;
}

Element.prototype.setWidth = function(width){
    var _this = this;
    var elementWidth;
    //si es container, update de width de sus childs containers
    if(this instanceof Container){
        jQuery(this.jID()).css('width',width);
        var width = jQuery(this.jID()).width();
        var nElements = _this._elements.length;
        var padding = _this._fixWidth;
        for(i in _this._elements){
            var element = _this._elements[i];
            var border = parseInt(jQuery(element.jID()).css('border-width'));
            if(_this.layout == "vbox" || !element.layout){
                elementWidth = width-(2*padding)-2*border;
            }
            else{
                if(_this.layout == "hbox"){
                    
                    width2 = width-(2*padding)-2*border;
                    elementWidth = width2/nElements;
//                    console.log("container: "+width2+", element: "+elementWidth+", n: "+nElements)
                }

            }
            element.setWidth(elementWidth);
        }
    }
    else{
        if(this._parent instanceof Container && this._parent.layout == 'hbox'){
//            jQuery(this.jID()).css('width',width);
//            this.width = width;
        }
    }
}

Element.prototype.getPxWidth = function(){
    jQuery(this.jID()).width();
}

Element.prototype.setHeight = function(height){
    var jId = "#"+this.id;
    jQuery(jId).css('height',height);
}

Element.prototype.getHtmlElement = function(){
    return jQuery("#"+this.id);
}
/*
* @param {create} Configure the new Element with the options object
* @returns {String} String Identificador unico interno
*/
Element.prototype.makeUId = function(){
    _idCounter++;
    return "RCK-"+_idCounter;
}
/*
* @param {create} Configure the new Element with the options object
* @param {options} Objeto configuracion
* @returns {void} No Retorna nada
*/

Element.prototype.setOptions = function(options){
    var _this = this;
    jQuery.each(options, function(i,item){
        _this[i] = options[i];
    });
    
    if(!this.id){
        this.id = this.makeUId();
    }
    
    this.Classes = [];
    
    
    return;
}

Element.prototype._getClassesString = function(array){
    if(!array)
        return this.Classes.join(" ");
    else return array.join(" ");
}

Element.prototype.cfgDeep = function(){
    var deep = 1;
    var _this = this;
    var go = true;
    while(_this._parent){
        _this = _this._parent;
        deep++;
       
    }
    _this._deep = deep;
    return;
}

function Element(options){
//    this.create(options);
}